import axios from 'axios';
import qs from 'qs';//转换请求参数格式，需要时使用
axios.defaults.baseURL = 'https://www.hangangzao.com';
// axios.defaults.baseURL =  '/api';

//http://scun-yun-oos1.oss-cn-shanghai.aliyuncs.com/dsms/doc
axios.defaults.timeout = 5000;

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么，例如加入token
    //config.headers.Authorization = '123';
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 在接收响应做些什么，例如跳转到登录页
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});
//post 请求上传header
export function postHead(url,params){
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: url,
            headers:{
                'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
                'Authorization':localStorage.getItem('token')
            },
            data:qs.stringify(params)
        }).then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            });
    });
}
//返回一个Promise(发送post请求)
export function postForm(url,params){
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(params),{
            'content-type': 'application/x-www-form-urlencoded'
        }).then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            });
    });
}
//返回一个Promise(发送post请求)
export function postJson(url,data){
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: url,
            headers:{
                'Content-type': 'application/json',
                'Authorization':localStorage.getItem('token')
            },
            data:data
        }).then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            });
    });
}
//post 请求上传header
export function postData(url,data){
    return new Promise((resolve, reject) => {
        axios({
            method: 'post',
            url: url,
            headers:{
                'Content-type': 'multipart/form-data',
                'Authorization':localStorage.getItem('token')
            },
            data:data
        }).then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            });
    });
}
//返回一个Promise(发送get请求)
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: param,
            headers:{ 'authorization': localStorage.getItem('token') }
        }).then(response => {
                resolve(response.data)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
export default {
    fetchGet,
    postForm,
    postHead,
    postData,
    postJson
}